package com.pkk.cloud.support.oauth2.config.configuration;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @description: web端请求安全的配置
 * @author: peikunkun
 * @create: 2020-03-19 15:48
 **/
@Configuration
public class WebSecurityAccessConfiguration extends WebSecurityConfigurerAdapter {

  /**
   * @Description: 创建认证处理器
   * @return: org.springframework.security.authentication.AuthenticationManager
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 3:48
   */
  @Override
  @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
  public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
  }

  /**
   * @Description: 加密方式
   * @return: org.springframework.security.crypto.password.PasswordEncoder
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 3:48
   */
  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }

  /**
   * @Description: 跨越操作配置
   * @return: org.springframework.boot.web.servlet.FilterRegistrationBean
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 3:49
   */
  @Bean
  public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);
    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(0);
    return bean;
  }

}
